﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Raw1;
using Raw1.DTOs;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MonitorManager manager = new MonitorManager();

            manager.MonitorAddedEvent += manager_MonitorAddedEvent;
            manager.MonitoringStaredEvent += manager_MonitoringStaredEvent;
            manager.MonitoringStoppedEvent += manager_MonitoringStoppedEvent;
            manager.MonitorRemovedEvent += manager_MonitorRemovedEvent;
            manager.MonitorChangedEvent += manager_MonitorChangedEvent;
            //manager.

            IMonitor fsMonitor = MonitorFactory.CreateMonitor(Monitors.FileSystem);
            manager.Subscribe(fsMonitor);
            manager.Start();

            listView1.FullRowSelect = true;

            if (manager.IsRunning)
            {
                //MessageBox.Show("Running");
                listView1.Items.Add("Running...");
            }
            else
            {
                //MessageBox.Show("Stopped Running");
                listView1.Items.Add("Stopped Running");
            }

            UpdateGrid(manager);
        }

        void manager_MonitorChangedEvent(object sender, EventArgs e)
        {
            //MessageBox.Show(sender.ToString(), "Changed");
            if (e is FileSystemMonitorArgs)
            {
                FileSystemMonitorArgs args = (FileSystemMonitorArgs)e;
                FileSystemMonitor _monitor = sender as FileSystemMonitor;
                if (_monitor != null)
                {
                    FileSystemDTO file =_monitor.Files.FirstOrDefault(x => x.FullName == args.FullPath);
                    if (file != null)
                    {
                        if (args.ChangeType == WatcherChangeTypes.Created)
                        {
                            file.Message = String.Format("This file is just created @ {0}", file.Created);
                        }
                        if (args.ChangeType == WatcherChangeTypes.Deleted)
                        {
                            file.Message = String.Format("This file is just Deleted @ ");
                        }
                    }
                }


                //listView1.Items.Add(string.Format("File: {0} {1} at {2}", args.ChangeType, args.Name, args.FullPath));
            }
            else
            {
                listView1.Items.Add(sender.ToString(), "Changed");
            }

        }

        void manager_MonitorRemovedEvent(object sender, MonitorEventArgs e)
        {
            listView1.Items.Add("Removed");
        }

        void manager_MonitoringStoppedEvent(object sender, EventArgs e)
        {
            listView1.Items.Add("Stopped");
        }

        void manager_MonitoringStaredEvent(object sender, EventArgs e)
        {
            listView1.Items.Add("Stared");
        }

        void manager_MonitorAddedEvent(object sender, MonitorEventArgs e)
        {
            listView1.Items.Add("Added");
        }


        private void UpdateGrid(MonitorManager manager)
        {
            //dataGridView1.ColumnCount = 10;
            dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
            dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font(dataGridView1.Font, FontStyle.Bold);

            //dataGridView1.Size = new Size(500, 250);
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dataGridView1.GridColor = Color.Black;
            dataGridView1.RowHeadersVisible = false;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.MultiSelect = false;
            //dataGridView1.Dock = DockStyle.Fill;

            BindingList<FileSystemDTO> bindingList = null;
            BindingSource source = null;
            foreach (var _monitor in manager.Monitors)
            {
                if (_monitor is FileSystemMonitor)
                {
                    bindingList = new BindingList<FileSystemDTO>(((FileSystemMonitor)_monitor).Files);
                    source = new BindingSource(bindingList, null);
                }
            }
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.ColumnCount = 5;
            dataGridView1.Columns[0].Name = "Directory";
            dataGridView1.Columns[0].DataPropertyName = "Directory";
            
            dataGridView1.Columns[1].Name = "FileName";
            dataGridView1.Columns[1].DataPropertyName = "FileName";
            
            dataGridView1.Columns[2].Name = "Created";
            dataGridView1.Columns[2].DataPropertyName = "Created";
            
            dataGridView1.Columns[3].Name = "Modified";
            dataGridView1.Columns[3].DataPropertyName = "Modified";
            
            dataGridView1.Columns[4].Name = "Message";
            dataGridView1.Columns[4].DataPropertyName = "Message";


            dataGridView1.DataSource = source;
        }
    }
}
